package com.wd.live.view.fragment;


import androidx.lifecycle.Observer;

import com.google.android.material.tabs.TabLayout;
import com.wd.live.R;
import com.wd.live.base.BaseFragment;
import com.wd.live.bean.FindLeaderBoardBean;
import com.wd.live.databinding.ListFragmentBinding;
import com.wd.live.view.CardItem;
import com.wd.live.view.ShadowTransformer;
import com.wd.live.view.adpater.CardPagerAdapter;
import com.wd.live.viewmodel.MainViewModel;

import java.util.List;

/**
 * <p>项目名称:LookFor2.0</p>
 * <p>包名:com.bawei.lookfor20.view.fragment</p>
 * <p>简述:</p>
 *
 * @author 孟令高
 * @date 2022/11/9
 */
public class ListFragment extends BaseFragment<MainViewModel, ListFragmentBinding> {
    String name[]={"最热主播榜","最佳新人榜"};
    private int position;
    private CardPagerAdapter adapter;
    private ShadowTransformer transformer;



    @Override
    protected void initData() {
        if(databind.tab.getTabCount()==0){
            for (String s : name) {
                databind.tab.addTab(databind.tab.newTab().setText(s));
            }
        }


        //进来先请求榜单
        getbangdan();
        databind.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                position = tab.getPosition();
                //点击切换再次请求榜单
                getbangdan();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }




    @Override
    protected int initLayout() {
        return R.layout.list_fragment;
    }

    @Override
    public void onChanged(Object o) {

    }
    private void getbangdan() {
        viewmodel.getfindLeaderBoard().observe(getActivity(), new Observer<FindLeaderBoardBean>() {
            @Override
            public void onChanged(FindLeaderBoardBean findLeaderBoardBean) {
                FindLeaderBoardBean.ResultBean result = findLeaderBoardBean.getResult();
                if(position==0){
                    //接口请求的网路数据集合
                    List<FindLeaderBoardBean.ResultBean.HotTopListBean> topList = result.getHotTopList();
                    //new卡片布局的适配器
                    adapter = new CardPagerAdapter();
                    //循环传递数据
                    for (int i = 0; i < topList.size(); i++) {
                        adapter.addCardItem(new CardItem(topList.get(i).getNickName(),
                                topList.get(i).getRank(),
                                topList.get(i).getHeadPic(),
                                topList.get(i).getFanSum(),
                                topList.get(i).getPersonalImage(),
                                topList.get(i).getSex(),
                                topList.get(i).getLiveStatus(),
                                topList.get(i).getLiveVideoTitle(),
                                topList.get(i).getLiveVideoTime(),
                                topList.get(i).getLiveVideoDuration()));
                    }
                    transformer = new ShadowTransformer(databind.vp, adapter);
                    transformer.enableScaling(true);


                    databind.vp.setAdapter(adapter);

                    databind.vp.setPageTransformer(false, transformer);

                    databind.vp.setOffscreenPageLimit(3);
                }else {


                    List<FindLeaderBoardBean.ResultBean.NewcomerTopListBean> mylist2 = result.getNewcomerTopList();

                    adapter = new CardPagerAdapter();
                    for (int i = 0; i < mylist2.size(); i++) {
                        adapter.addCardItem(new CardItem(mylist2.get(i).getNickName(),
                                mylist2.get(i).getRank(),
                                mylist2.get(i).getHeadPic(),
                                mylist2.get(i).getFanSum(),
                                mylist2.get(i).getPersonalImage(),
                                mylist2.get(i).getSex(),
                                mylist2.get(i).getLiveStatus(),
                                mylist2.get(i).getLiveVideoTitle(),
                                mylist2.get(i).getLiveVideoTime(),
                                mylist2.get(i).getLiveVideoDuration()));

                    }
                    transformer = new ShadowTransformer(databind.vp, adapter);
                    transformer.enableScaling(true);

                    databind.vp.setAdapter(adapter);
                    databind.vp.setPageTransformer(false, transformer);
                    databind.vp.setOffscreenPageLimit(3);
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}